package com.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

/**
 * @Author: YLBG-YCY-1325
 * @Description: java8学习
 * @Date: 2017/7/10
 */
public class J8Test {
    private static boolean is ;
    public static void main(String[] args) {
        List<Person> p = new ArrayList<>();
        p.add(new Person("lilei", 12));
        p.add(new Person("lilei", 22));
        p.add(new Person("miss", 13));
        Predicate<Person> personPredicate = (person) -> "lilei".equals(person.getName());
        List<Person> persons = sort(p,
                personPredicate.and((person) -> person.getAge() == 22).negate());
        System.out.println(p.stream().map(Person::getAge).reduce(0, (a, b) -> a + b));
        System.out.println(persons.size() + ":" + persons.get(0).getAge());
        p.sort(comparing(Person::getAge));
        // IntStream.iterate(0, n -> n +
        // 4).limit(5).boxed().forEach(System.out::print);
//        IntStream.rangeClosed(0, 100).forEach(i -> System.out.println(i + "->"));
        List<String> strs = Arrays.asList("hello", "world");
        List<String[]> strings = strs.stream().map(item -> item.split("")).distinct()
                .collect(Collectors.toList());
        strings.forEach(item -> Arrays.asList(item).stream().forEach(System.out::println));
        System.out.println(is);
    }

    public static List<Person> sort(List<Person> list, Predicate<Person> predicate) {
        List<Person> persons = new ArrayList<>();
        for (Person person : list) {
            if (predicate.test(person)) {
                persons.add(person);
            }
        }
        return persons;
    }
}
